﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Data;

using DataAccess;
using Entity;

namespace Bussiness
{
    public class BestRoutesManager
    {
        public bool BestRouteAdd(int crCode, int gid, string lons, string lats, string alts, string field, double total, int exitId, int courseID)
        {
            BestRoutesData br = new BestRoutesData();
            return br.BestRouteAdd(crCode, gid, lons, lats, alts, field, total, exitId,courseID);
        }
        public Collection<ERouteNode> BestRouteGetByCode(int crCode,int groupId)
        {           
            Collection<ERouteNode> nodes = new Collection<ERouteNode>();
            BestRoutesData br = new BestRoutesData();
            DataTable dt=br.BestRouteGetByCode(crCode,groupId);
            if (dt.Rows.Count != 0)
            {
                string lonstr = dt.Rows[0]["Lons"].ToString();
                string latstr = dt.Rows[0]["Lats"].ToString();
                string altstr = dt.Rows[0]["Alts"].ToString();
                string[] lonsArr = lonstr.Substring(0, lonstr.Length - 1).Split(',');
                string[] latsArr = latstr.Substring(0, latstr.Length - 1).Split(',');
                string[] altsArr = altstr.Substring(0, altstr.Length - 1).Split(',');
                for (int i = 0; i < latsArr.Length; i++)
                {
                    ERouteNode node = new ERouteNode(Double.Parse(lonsArr[i]), Double.Parse(latsArr[i]), Double.Parse(altsArr[i]));
                    nodes.Add(node);
                }
            }
            else
            {
                
            }
            return nodes;
        }
        public Collection<ERouteNode> BestRouteGetByCodeAndStatus(int crCode)
        {
            Collection<ERouteNode> nodes = new Collection<ERouteNode>();
            SysConfigsData sc = new SysConfigsData();
            int currentCode = Convert.ToInt32(sc.GetParaByName("LastGroupID"));
            BestRoutesData br = new BestRoutesData();
            DataTable dt = br.BestRouteGetByCodeAndStatus(crCode, currentCode);
            if (dt.Rows.Count > 0)
            {
                string lonstr = dt.Rows[0]["Lons"].ToString();
                string latstr = dt.Rows[0]["Lats"].ToString();
                string altstr = dt.Rows[0]["Alts"].ToString();
                string[] lonsArr = lonstr.Substring(0, lonstr.Length - 1).Split(',');
                string[] latsArr = latstr.Substring(0, latstr.Length - 1).Split(',');
                string[] altsArr = altstr.Substring(0, altstr.Length - 1).Split(',');
                for (int i = 0; i < latsArr.Length; i++)
                {
                    ERouteNode node = new ERouteNode(Double.Parse(lonsArr[i]), Double.Parse(latsArr[i]), Double.Parse(altsArr[i]));
                    nodes.Add(node);
                }
            }
            return nodes;
        }
        public bool UpdateRouteStatus(int crCode, int groupID)
        {
            BestRoutesData brd = new BestRoutesData();
            return brd.UpdateRouteStatus(crCode, groupID);
        }
        public DataTable FindRouteHistory(int crCode, DateTime start, DateTime end)
        {
            BestRoutesData br = new BestRoutesData();
            return br.FindRouteHistory(crCode, start, end);
        }
        public DataTable BestRouteGetByGroupId(int groupId, int status)
        {            
            BestRoutesData br = new BestRoutesData();
            return br.BestRouteGetByGroupId(groupId, status);            
        }

    }
}
